using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Hardware;


namespace eHouse
{
    class AddIrrigationRulesWindow:Window
    {
        Window prevwindow;
        private Window addIrrigationRulesWindow;
        Canvas canvas = new Canvas();
        Text text_time;
        bool state = true;
        private int hours = 12;
        private int min = 0;

        SimpleButton button_state_on;
        SimpleButton button_state_off;

        public AddIrrigationRulesWindow(Window lsw)
        {
            prevwindow = lsw;
            addIrrigationRulesWindow = new Window();
            addIrrigationRulesWindow.Width = SystemMetrics.ScreenWidth;
            addIrrigationRulesWindow.Height = SystemMetrics.ScreenHeight;

            CreateWindow();
        }

        private void CreateWindow()
        {
            string mins = min.ToString();
            text_time = new Text(Resources.GetFont(Resources.FontResources.NinaB), hours.ToString() + ":" + ((mins.Length < 2) ? ("0" + mins) : mins));
            text_time.HorizontalAlignment = HorizontalAlignment.Center;
            text_time.VerticalAlignment = VerticalAlignment.Center;
            Panel pan_time = new Panel();
            pan_time.Children.Add(text_time);
            Canvas.SetTop(pan_time, 0);
            Canvas.SetLeft(pan_time, 0);
            canvas.Children.Add(pan_time);

            Image image_up = new Image(Resources.GetBitmap(Resources.BitmapResources.arrow_up));
            image_up.HorizontalAlignment = HorizontalAlignment.Center;
            image_up.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_up = new SimpleButton(image_up, 55, 55);
            button_up.BackColor = Colors.White;
            Canvas.SetTop(button_up, 0);
            Canvas.SetRight(button_up, 0);
            canvas.Children.Add(button_up);

            Image image_down = new Image(Resources.GetBitmap(Resources.BitmapResources.arrow_down));
            image_down.HorizontalAlignment = HorizontalAlignment.Center;
            image_down.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_down = new SimpleButton(image_down, 55, 55);
            button_down.BackColor = Colors.White;
            Canvas.SetTop(button_down, 60);
            Canvas.SetRight(button_down, 0);
            canvas.Children.Add(button_down);

            Image image_state_on = new Image(Resources.GetBitmap(Resources.BitmapResources.switch_on));
            image_state_on.HorizontalAlignment = HorizontalAlignment.Center;
            image_state_on.VerticalAlignment = VerticalAlignment.Center;
            button_state_on = new SimpleButton(image_state_on, 50, 50);
            button_state_on.BackColor = Colors.White;
            Canvas.SetTop(button_state_on, addIrrigationRulesWindow.Height / 2);
            Canvas.SetLeft(button_state_on, 0);
            canvas.Children.Add(button_state_on);

            Image image_state_off = new Image(Resources.GetBitmap(Resources.BitmapResources.switch_off));
            image_state_off.HorizontalAlignment = HorizontalAlignment.Center;
            image_state_off.VerticalAlignment = VerticalAlignment.Center;
            button_state_off = new SimpleButton(image_state_off, 50, 50);
            button_state_off.BackColor = Colors.White;            

            Image image_accept = new Image(Resources.GetBitmap(Resources.BitmapResources.accept));
            image_accept.HorizontalAlignment = HorizontalAlignment.Center;
            image_accept.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_accept = new SimpleButton(image_accept, addIrrigationRulesWindow.Width / 2, addIrrigationRulesWindow.Height / 5);
            button_accept.BackColor = Colors.White;
            Canvas.SetBottom(button_accept, 0);
            Canvas.SetLeft(button_accept, 0);
            canvas.Children.Add(button_accept);

            Image image_return = new Image(Resources.GetBitmap(Resources.BitmapResources.imreturn));
            image_return.HorizontalAlignment = HorizontalAlignment.Center;
            image_return.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_return = new SimpleButton(image_return, addIrrigationRulesWindow.Width / 2, addIrrigationRulesWindow.Height / 5);
            button_return.BackColor = Colors.White;
            Canvas.SetBottom(button_return, 0);
            Canvas.SetRight(button_return, 0);
            canvas.Children.Add(button_return);

            button_up.Click += new EventHandler(buttonUp_Click);
            button_down.Click += new EventHandler(buttonDown_Click);
            button_state_off.Click += new EventHandler(buttonStateOff_Click);
            button_state_on.Click += new EventHandler(buttonStateOn_Click);
            button_accept.Click += new EventHandler(buttonAccept_Click);
            button_return.Click += new EventHandler(buttonReturn_Click);

            addIrrigationRulesWindow.Child = canvas;
            addIrrigationRulesWindow.Visibility = Visibility.Visible;
        }

       

        void buttonUp_Click(object sender, EventArgs evt)
        {
            min += 10;
            if (min == 60)
            {
                min = 0;
                hours++;
            }

            if (hours == 24)
                hours = 0;

            text_time.TextContent = hours + ":" + min;
        }

        void buttonDown_Click(object sender, EventArgs evt)
        {
            min -= 10;
            if (min == -10)
            {
                min = 50;
                hours--;
            }
            if (hours == -1)
                hours = 23;

            text_time.TextContent = hours + ":" + min;
        }

        void buttonStateOn_Click(object sender, EventArgs evt)
        {
            state = false;
            canvas.Children.Remove(button_state_on);

            Canvas.SetTop(button_state_off, addIrrigationRulesWindow.Height/2);
            Canvas.SetLeft(button_state_off, 0);
            canvas.Children.Add(button_state_off);
        }

        void buttonStateOff_Click(object sender, EventArgs evt)
        {
            state = true;
            canvas.Children.Remove(button_state_off);

            Canvas.SetTop(button_state_on, addIrrigationRulesWindow.Height / 2);
            Canvas.SetLeft(button_state_on, 0);
            canvas.Children.Add(button_state_on);
        }

        void buttonAccept_Click(object sender, EventArgs evt)
        {
            prevwindow.Close();

            string id = Guid.NewGuid().ToString().ToUpper();
            string mins = min.ToString();
            string time = hours.ToString() + ":" + ((mins.Length < 2) ? ("0" + mins) : mins);
             
            IrrigationBL.GetInstance().AddRule(new Rule(id, state, time));
            
            IrrigationScheduleWindow isw = new IrrigationScheduleWindow();
            addIrrigationRulesWindow.Close();
        }

        void buttonReturn_Click(object sender, EventArgs evt)
        {
            addIrrigationRulesWindow.Close();
        }
    }
}
